{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cef23281",
   "metadata": {},
   "source": [
    "## 1.np.random.rand() —— 生成均匀分布的随机数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02775ccc",
   "metadata": {},
   "source": [
    "功能：\n",
    "`np.random.rand()` 用于生成在 [0, 1) 区间内均匀分布的随机浮点数。\n",
    "\n",
    "语法：\n",
    "```python\n",
    "np.random.rand(d0, d1, ..., dn)\n",
    "```\n",
    "\n",
    "- **d0, d1, ..., dn**：指定生成随机数的形状，可以是任意维度的整数。\n",
    "- 生成的数组的每个元素是 [0, 1) 范围内的随机浮点数。    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4f18dd4",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1206f8e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.86545204 0.7205819  0.55805882]\n",
      " [0.75804106 0.99374162 0.9403146 ]]\n"
     ]
    }
   ],
   "source": [
    "# np.random.rand() 用于生成在 [0, 1) 区间内均匀分布的随机浮点数。\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "# 生成一个 2x3 的随机数组，每个数值都在 [0, 1) 范围内\n",
    "arr = np.random.rand(2,3)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78dea4c8",
   "metadata": {},
   "source": [
    "## 2. np.random.randn() —— 生成标准正态分布的随机数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7788b153",
   "metadata": {},
   "source": [
    "功能：\n",
    "`np.random.randn()` 用于生成标准正态分布（均值为0，标准差为1）的随机数。\n",
    "\n",
    "语法：\n",
    "```python\n",
    "np.random.randn(d0, d1, ..., dn)\n",
    "```\n",
    "\n",
    "- **d0, d1, ..., dn**：指定生成随机数的形状，类似于 `np.random.rand()`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4c6ec325",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.12999773  0.27836294 -0.13506252]\n",
      " [ 1.62499321 -0.56483697  0.49104322]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# np.random.randn() 用于生成标准正态分布（均值为0，标准差为1）的随机数。\n",
    "# 生成一个 2x3 的标准正态分布随机数组\n",
    "arr = np.random.randn(2,3)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2eaaa640",
   "metadata": {},
   "source": [
    "## 3. np.random.randint() —— 生成指定范围的整数随机数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fd2a514",
   "metadata": {},
   "source": [
    "功能：\n",
    "`np.random.randint()` 用于生成在指定范围内的随机整数。\n",
    "\n",
    "语法：\n",
    "```python\n",
    "np.random.randint(low, high=None, size=None, dtype=int)\n",
    "```\n",
    "\n",
    "- **low**：生成随机整数的下限（包含）。\n",
    "- **high**：生成随机整数的上限（不包含）。如果没有提供 `high`，则默认为 `low`，生成范围是 `[0, low)`。\n",
    "- **size**：指定返回数组的形状。\n",
    "- **dtype**：指定输出数组的数据类型，默认为 `int`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e9cf1760",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[8 9 1]\n",
      " [3 8 6]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 生成一个 2x3 的随机整数数组，范围在 [0, 10) 之间\n",
    "arr = np.random.randint(1,10,(2,3))\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f26c79e",
   "metadata": {},
   "source": [
    "## 4.np.random.normal() —— 生成正态分布的随机数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6045dbd2",
   "metadata": {},
   "source": [
    "功能：\n",
    "`np.random.normal()` 用于生成符合正态分布（高斯分布）的随机数。可以指定均值（mean）和标准差（std）。\n",
    "\n",
    "语法：\n",
    "```python\n",
    "np.random.normal(loc=0.0, scale=1.0, size=None)\n",
    "```\n",
    "\n",
    "- **loc**：正态分布的均值，默认为 0。\n",
    "- **scale**：正态分布的标准差，默认为 1。\n",
    "- **size**：输出数组的形状。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2e126212",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.28959514  0.52261162 -0.75054527]\n",
      " [-1.45789057  1.53623386 -0.97119921]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 生成一个 2x3 的正态分布随机数，均值为 0，标准差为 1\n",
    "arr = np.random.normal(0,1,(2,3))\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ada08ca0",
   "metadata": {},
   "source": [
    "## 5. np.random.standard_normal() —— 生成标准正态分布的随机数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5a9b521",
   "metadata": {},
   "source": [
    "功能：\n",
    "`np.random.standard_normal()` 用于生成标准正态分布（均值为 0，标准差为 1）的随机数，功能上与 `np.random.randn()` 相似。\n",
    "\n",
    "语法：\n",
    "```python\n",
    "np.random.standard_normal(size=None)\n",
    "```\n",
    "\n",
    "- **size**：输出数组的形状。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82ac6569",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-3.03789891  0.88031505 -0.61742754]\n",
      " [-0.21727936  0.66205028 -0.00743258]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 生成一个 2x3 的标准正态分布随机数数组\n",
    "arr = np.random.standard_normal((2,3))\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7512621",
   "metadata": {},
   "source": [
    "## 6. np.random.uniform() —— 生成均匀分布的随机数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbd46cd4",
   "metadata": {},
   "source": [
    "功能：\n",
    "`np.random.uniform()` 用于生成在指定区间内均匀分布的随机浮点数。\n",
    "\n",
    "语法：\n",
    "```python\n",
    "np.random.uniform(low=0.0, high=1.0, size=None)\n",
    "```\n",
    "\n",
    "- **low**：生成随机数的下限，默认为 0。\n",
    "- **high**：生成随机数的上限，默认为 1。\n",
    "- **size**：输出数组的形状。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c577dc03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3.64091419 7.63468688 1.4194384 ]\n",
      " [4.73355653 4.72253718 1.8934116 ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 生成一个 2x3 的均匀分布随机数组，范围在 [0, 10) 之间\n",
    "arr = np.random.uniform(0, 10, size=(2, 3))\n",
    "print(arr)\n",
    "# 输出: [[2.47651913 7.26796946 1.09987281]\n",
    "#        [3.02770916 4.05445851 8.35904917]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aaa40d7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "从数组中随机选择: [1 4 5]\n",
      "从0-9中选择: [5 8 4 8 1]\n",
      "不重复选择: [2 3 1]\n",
      "带权重选择: [4 2 4 2 5]\n",
      "随机选择水果: ['orange' 'orange']\n",
      "多维结果:\n",
      " [[3 3 2]\n",
      " [1 5 1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# np.random.choice() 用法详解\n",
    "\n",
    "# 1. 从一维数组中随机选择元素\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "result = np.random.choice(arr, size=3)  # 随机选择3个元素（允许重复）\n",
    "print(\"从数组中随机选择:\", result)\n",
    "\n",
    "# 2. 从整数范围中选择\n",
    "result = np.random.choice(10, size=5)  # 从0-9中随机选择5个数\n",
    "print(\"从0-9中选择:\", result)\n",
    "\n",
    "# 3. 不允许重复选择\n",
    "result = np.random.choice(arr, size=3, replace=False)  # 不重复选择\n",
    "print(\"不重复选择:\", result)\n",
    "\n",
    "# 4. 带权重的随机选择\n",
    "weights = [0.1, 0.2, 0.3, 0.3, 0.1]  # 对应arr中每个元素的选择概率\n",
    "result = np.random.choice(arr, size=5, p=weights)\n",
    "print(\"带权重选择:\", result)\n",
    "\n",
    "# 5. 从字符串数组中选择\n",
    "fruits = np.array(['apple', 'banana', 'orange', 'grape'])\n",
    "result = np.random.choice(fruits, size=2)\n",
    "print(\"随机选择水果:\", result)\n",
    "\n",
    "# 6. 生成多维结果\n",
    "result = np.random.choice(arr, size=(2, 3))  # 生成2x3的数组\n",
    "print(\"多维结果:\\n\", result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e7456ca",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml311",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
